package com.shop.web.admin;

import com.shop.entity.OperLog;
import com.shop.entity.User;
import com.shop.entity.pojo.ResultBean;
import com.shop.service.OperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/operlog")
public class OperLogController {
    @Autowired
    private OperLogService operLogService;

    /**
     * 打开用户列表页面
     * @return
     */
    @RequestMapping("/toList.html")
    public String toList() {
        return "admin/operlog/list";
    }

    /**
     * 获取所有用户列表
     *
     * @param pageindex
     * @return
     */
    @ResponseBody
    @RequestMapping("/list.do")
    public ResultBean<List<OperLog>> findAllOperlog(int pageindex,
                                              @RequestParam(value = "pageSize", defaultValue = "15") int pageSize) {
        Pageable pageable = new PageRequest(pageindex, pageSize, null);
        List<OperLog> operLogs = operLogService.findAll(pageable).getContent();
        return new ResultBean<>(operLogs);
    }

    @ResponseBody
    @RequestMapping("/getTotal.do")
    public ResultBean<Integer> geTotal() {
        Pageable pageable = new PageRequest(1, 15, null);
        int total = (int) operLogService.findAll(pageable).getTotalElements();
        return new ResultBean<>(total);
    }
}
